PowerGrad

计算 Power的梯度。 对每个输入元素执行线性变换,并根据公式计算梯度结果。

\[output_i = power \times scale \times Input1_i \times (scale \times Input2_i + shift)^{(power - 1)}\]
输入:
  • Input1 - 第一个输入数据地址,对应前向传播的输入。

  • Input2 - 第二个输入数据地址,对应前向传播的输入。

  • length - 输入长度。

  • power - 幂指数。

  • scale - 缩放系数。

  • shift - 偏移值。

  • core_mask(int, 可选) - 核掩码(仅适用于共享存储版本)。

输出:
  • output - 梯度计算结果地址。

支持平台:

FT78NE MT7004

备注

  • FT78NE 支持 fp32

  • MT7004 支持 fp32, fp16

  • scale 为 0,则输出恒为 0。

共享存储版本:

void fp_power_grad_s(float *Input1, float *Input2, float *output, int length, float power, float scale, float shift, int core_mask)
void hp_power_grad_s(half *Input1, half *Input2, half *output, int length, float power, float scale, float shift, int core_mask)

C调用示例:

 1//FT78NE示例
 2#include <stdio.h>
 3
 4int main(int argc, char* argv[]) {
 5    float *input1 = (float *)0xA0000000;   // input1 在 DDR 空间
 6    float *input2 = (float *)0xA1000000;   // input2 在 DDR 空间
 7    float *output = (float *)0xB0000000;   // 输出结果在 DDR 空间
 8    int length = 1024;
 9    float power = 2.0f, scale = 0.5f, shift = 1.0f;
10    int core_mask = 0xff;
11    fp_power_grad_s(input1, input2, output, length, power, scale, shift, core_mask);
12    return 0;
13}

私有存储版本:

void fp_power_grad_p(float *Input1, float *Input2, float *output, int length, float power, float scale, float shift)
void hp_power_grad_p(half *Input1, half *Input2, half *output, int length, float power, float scale, float shift)

C调用示例:

 1//MT7004示例
 2#include <stdio.h>
 3
 4int main(int argc, char* argv[]) {
 5    float *input1 = (float *)0x10000000;
 6    float *input2 = (float *)0x10001000;
 7    float *output = (float *)0x10002000;
 8    int length = 1024;
 9    float power = 2.0f, scale = 0.5f, shift = 1.0f;
10    fp_power_grad_p(input1, input2, output, length, power, scale, shift);
11    return 0;
12}